
class IyfSearch extends AbstractPlugin {

  static BASE_URL = 'https://www.iyf.tv';

  async fetchSearchResults(query) {
    const searchUrl = `https://rankv21.iyf.tv/v3/list/briefsearch?tags=${encodeURIComponent(query)}&orderby=4&page=1&size=36&desc=1&isserial=-1`
    const http_headers = {
      'User-Agent': IyfSearch.DEFAULT_USER_AGENT,
      'Origin': IyfSearch.BASE_URL,
    };
    const result  = await doHttpRequestSync(searchUrl, { headers: http_headers });
    if(result.status !== 'success' || /error code/.test(result.body)) {
      return null;
    }
    const data = result.body;

    const searchResults = [];

    const results = JSON.parse(data);
    if(results.hasOwnProperty('data') && results.data.hasOwnProperty('info') && Array.isArray(results.data.info)
      && results.data.info.length > 0 && results.data.info[0].hasOwnProperty('result') > 0) {
      results.data.info[0].result.forEach(item => {
        if(!item.hasOwnProperty('title') && !item.hasOwnProperty('contxt')) return;

        const searchResult = new SearchResult();
        searchResult.site_icon = '/plugins/iyf/icon.png';
        searchResult.site_name = IyfSearch.BASE_URL.replace(/https?:\/\/(www\.)?/, '');

        searchResult.title = item.title;
        searchResult.link = `${IyfSearch.BASE_URL}/play/${item.contxt}`;
        searchResult.image = item.imgPath;

        // searchResult.vid = item.contxt;
        // searchResult.cid = item.cid;

        const infos = [];
        if(item.hasOwnProperty('atypeName')) {
          infos.push(`类型: ${item['atypeName']}`);
        }
        if(item.hasOwnProperty('regional')) {
          infos.push(`国家: ${item['regional']}`);
        }
        if(item.hasOwnProperty('postTime')) {
          infos.push(`上映时间: ${item['postTime']}`);
        }
        if(item.hasOwnProperty('lastName')) {
          infos.push(`集数: ${item['lastName']}`);
        }
        searchResult.description = infos.join('\n');

        searchResults.push(searchResult);
      });
    }
    return searchResults;
  }
}
console.log("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
Registry.registerClass("IyfSearch", IyfSearch.constructor);